'use strict'

define(['jquery', 'bootstrap', 'bootstrapTable', 'bootstrapTableCN', 'datetimepicker', 'datetimepickerCN', 'select2', 'validate', 'validate.en', 'shiftform',], function($) {
  
  return {
    init: function(data) {
        this.initPage();
        this.exportReport(data.today);
    },
    
    initPage: function(){
        $('select[name="site"]').select2({
            minimumResultsForSearch : -1,
            allowClear : true,
            width: '150'
        });

        $('select[name="site"]').change(function(){ 
            var site = $(this).children('option:selected').val();
            window.location.href = window.__CTX__.root + "/unfinished-round-reports?site=" + site;
        });
    },
    
    exportReport: function(today) {
        $(document).on('shown.bs.modal', '#report-export', function(e){
            $(e.currentTarget).find('form[data-validate="true"]').bt_validate();  //bind validate

            $('select[name="siteId"]').select2({
                placeholder : '请选择',
                minimumResultsForSearch : -1,
                allowClear : true,
                width: '100%'
            });

            $('select[name="coachId"]').select2({
                placeholder : '请选择教练',
                minimumResultsForSearch : 5,
                allowClear : true,
            });
            
            $('.beginDate').datetimepicker({
                format: 'yyyy-mm-dd',
                autoclose: true,
                language: 'zh-CN',
                todayBtn: 'linked',
                todayHighlight: true,
                pickerPosition: 'bottom-right',
                minView: 'month',
                startDate: '2018-07-17',
                endDate: today
              }).on('hide', function(event) {  
                event.preventDefault();  
                event.stopPropagation();  
                var startTime = event.date;  
                $(".endDate").datetimepicker('setStartDate',startTime);  
                $(".endDate").val("");  
            });
            
            $(".endDate").datetimepicker({  
                format: 'yyyy-mm-dd',
                autoclose: true,
                language: 'zh-CN',
                todayBtn: 'linked',
                todayHighlight: true,
                pickerPosition: 'bottom-right',
                minView: 'month',
                startDate: '2018-07-17',
                endDate: today
            }).on('hide', function(event) {  
                event.preventDefault();  
                event.stopPropagation();  
                var endTime = event.date;  
                $(".beginDate").datetimepicker('setEndDate',endTime);  
            });

            $('select[name="siteId"]').on('change', function() {
                $('.coach').html('<select name="coachId" class="form-control"><option value="">全部教练</option>');
                
                var siteId = $(this).val();
                $.ajax({
                    type: 'POST',
                    url: window.__CTX__.root + '/unfinished-round-reports/coaches',
                    data: {'siteId': siteId},
                    success: function(res) {
                        res = JSON.parse(res);
                        if(res.data.coachAmount > 0) {
                            var coachSelect = '<select name="coachId" class="form-control"><option value="">全部教练</option>';
                            for(var index in res.data.coaches) {
                                coachSelect = coachSelect + '<option value="' + res.data.coaches[index].id + '">' + res.data.coaches[index].name + '</option>'
                            }
                            coachSelect = coachSelect + '</select>';
                            $('.coach').html(coachSelect);
                            
                            $('select[name="coachId"]').select2({
                                minimumResultsForSearch : 5,
                                allowClear : true,
                            });
                        }
                    }
                });
            });
            
            $('button[type="submit"').click(function() {
                $(e.currentTarget).find("form").trigger("validate.ajax.submit");
                $.bt_validate.result[$.bt_validate.form_id];
            });
        });
    }
  }
})
